	//
	//  Audio.swift
	//  hello
	//
	//  Created by 赵康 on 2024/11/11.
	//

import Fluent
import Vapor

final class Audio: Model, Content, @unchecked Sendable {
	static var schema: String { SchemaEnum.audios.rawValue }
	
	@ID(key: .id)
	var id: UUID?
	
	@Field(key: FieldKeys.title)
	var title: String
	
	@OptionalField(key: FieldKeys.description)
	var description: String?
	
	@Field(key: FieldKeys.duration)
	var duration: Double
	
	@OptionalField(key: FieldKeys.cover)
	var cover: String?
	
	@OptionalField(key: FieldKeys.author)
	var author: String?
	
	@Field(key: FieldKeys.url)
	var url: String
	
	@Field(key: FieldKeys.size)
	var size: Double
	
	@OptionalField(key: FieldKeys.isDeleted)
	var isDeleted: Bool?
	
	@Timestamp(key: FieldKeys.createdAt, on: .create)
	var createdAt: Date?
	
	@Timestamp(key: FieldKeys.updatedAt, on: .update)
	var updatedAt: Date?
	
	@Parent(key: FieldKeys.album)
	var album: Album
	
	@Children(for: \.$audio)
	var transcripts: [Transcript]
	
	@Children(for: \.$audio)
	var histories: [History]
	
	@Children(for: \.$audio)
	var notes: [Note]
	
	init() { }
	
	init(
		id: UUID? = nil,
		title: String,
		description: String?,
		duration: Double,
		cover: String?,
		author: String?,
		url: String,
		size: Double,
		isDeleted: Bool?,
		createdAt: Date?,
		updatedAt: Date?,
		albumID: Album.IDValue
	) {
		self.id = id
		self.title = title
		self.description = description
		self.duration = duration
		self.cover = cover
		self.author = author
		self.url = url
		self.size = size
		self.isDeleted = isDeleted
		self.createdAt = createdAt
		self.updatedAt = updatedAt
		self.$album.id = albumID
	}
}
	// MARK: - 用于搜索的扩展
struct AudioSearchResponse: Content {
	let id: UUID?
	let title: String
	let description: String?
	let duration: Double
	let cover: String?
	let author: String?
	let url: String
	let size: Double
	let albumID: UUID?
	let albumTitle: String
	let albumCover: String?
	let createdAt: Date?
	let updatedAt: Date?
}

struct Recent7DaysAudiosCountResponse: Content {
  let audioCount: Int
  let exceedPercentage: Double
  
  var formattedExceedPercentage: String {
    String(format: "%.1f%%", exceedPercentage)
  }
}

extension Audio {
	struct Create: Content {
		struct TranscriptDTO: Content {
			let sentence: String
			let translation: String
			let startTime: TimeInterval
			let endTime: TimeInterval
		}
		
		let title: String
		let description: String?
		let duration: Double
		let cover: String?
		let author: String?
		let url: String
		let size: Double
		let albumID: UUID
		let transcripts: [TranscriptDTO]
	}
}